package com.lin.lostandfound;


import okhttp3.*;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;

public class HTTP {

	/*@Test
	public void context(){
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder().get().url("https:www.baidu.com").build();
		Call call = client.newCall(request);
		try {
			Response response = call.execute();
			ResponseBody body = response.body();
			System.out.println(body.string());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}*/

    /**
     * http://work.ecaray.com/data?comid=000000320&entryid=2017101615050941989521 0295501677
     * &equipmentip=192.168.0.1&equipmentid=2017101615050943679813533 2888420
     * &intime=1508396722885&method=uploadInPlateNumber
     * &module=upp&m s=1508396722885
     * &parkinglotid=20171012182423037868215442807736
     * &platenumb er=%E7%B2%A4N02181&
     * service=Device&ve=2&sign=c5eab1bcaebef8dd49672cb892b26ea7
     * http://114.116.228.110/MYTC/Pages/Proj/proj_ScreenShow/list.html
     */
    @Test
    public void context(){
        OkHttpClient client = new OkHttpClient();
        FormBody form = new FormBody.Builder()
                .add("comid","000000320")
                .add("entryid","20171016150509419895210295501677")
                .add("equipmentip","192.168.0.1")
                .add("equipmentid","20171016150509436798135332888420")
                .add("intime","1508396722885")
                .add("method","uploadInPlateNumber")
                .add("module","upp")
                .add("ms","1508396722885")
                .add("parkinglotid","20171012182423037868215442807736")
                .add("platenumber","鲁BY382T")
                .add("service","Device")
                .add("ve","2")
                .add("sign","c5eab1bcaebef8dd49672cb892b26ea7")
                .build();
        Request request = new Request.Builder().url("http://work.ecaray.com/data").post(form).build();
        Call call = client.newCall(request);
        try {
            Response response = call.execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*@Test
    public void context(){
        OkHttpClient client = new OkHttpClient();
        FormBody formBody = new FormBody.Builder()
                .add("username", "admin")
                .add("password", "admin")
                .build();
        final Request request = new Request.Builder()
                .url("http://127.0.0.1:8081/testPost")
                .post(formBody)
                .build();
        Call call = client.newCall(request);

        try {
            Response response = call.execute();
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/
	/*class CallTest implements Callback {
		@Override
		public void onFailure(@NotNull Call call, @NotNull IOException e) {

		}

		@Override
		public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

		}
	}*/
    @Test
    public void md5(){

        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        String str = "comid=000000320&entryid=20171016150509419895210295501677&equipmentid=20171016150509436798135332888420&equipmentip=192.168.0.1&intime=1508396722885&method=uploadInPlateNumber&module=upp&ms=1508396722885&parkinglotid=20171012182423037868215442807736&platenumber=粤N02181&service=Device&ve=2&requestKey=D3029C73406221B02026B684BB00579C";
        md.update(str.getBytes());

        str = new BigInteger(1, md.digest()).toString(16);

        System.out.println(str);
    }
    @Test
    public void put(){
        HashMap<String,String> m = new HashMap<>();
        m.put("comid","000000320");
        m.put("entryid","20171016150509419895210295501677");
        m.put("equipmentid","20171016150509436798135332888420");
        m.put("equipmentip","192.168.0.1");
        m.put("intime","1508396722885");
        m.put("method","uploadInPlateNumber");
        m.put("module","upp");
        m.put("ms","1508396722885");
        m.put("parkinglotid","20171012182423037868215442807736");
        m.put("platenumber","粤N02181");
        m.put("service","Device");
        m.put("ve","2");
        String ms = "";
        Object[] objects = m.keySet().toArray();
        Arrays.sort(objects);
        for (Object item : objects){
            ms = ms +item+"="+ m.get(item)+"&";
        }
        System.out.println(ms);
    }
}

